import {aliPay,getAccount,getCartByUsername} from "@/api/api";
import {getAllOrders, postOrder} from "../../../src/api/api";
import {compile} from "vue-template-compiler";
let nowUsername= sessionStorage.getItem("nowUsername");
export default {
  data(){
    return{
      totalPrice:0,
      //----测试用----start
      orderPay: {
        orderId: 6,//后端OrderPayVO是String
        totalPrice: 52.13,
        orderName: "测试001",
        orderDate: "1992-04-09 12:20:03"
      },
      //----测试用----end
      user:{
        buyerName: "st",
        address : "zn",
        city : "changsha",
        courier:"圆通",
        country : "China",
        phone : "12345",
        payType: "Alipay"
      },
      newOrderList:[
        {
          name: "golden dog",
          quantity : 2,
          unitPrice: 130,
          totalPrice : 260,
          image : "../../static/picture/cart-1.jpg"
        }
      ],
      newOrder: {
        username: "",
        realname: "",
        orderDate: "",
        orderStatus: "NF",
        shipCountry: "China",
        shipCity: "",
        address: "",
        courier: "",
        totalPrice: 0,
        payType: "AliPay",
        itemList: [

        ]
      }
    }
  },
  mounted() {
    this.getNewOrderList();
    this.getOrderId();
    getAccount().then((res) => {
      if (res.data.status == 0) {
        console.log(res);
        this.newOrder.username = res.data.data.username;
        this.newOrder.realname = res.data.data.realname;
        this.newOrder.address = res.data.data.address1;
        this.newOrder.shipCountry = res.data.data.country;
        this.newOrder.shipCity = res.data.data.city;
        this.newOrder.phone = res.data.data.phone;

      } else {
        alert("请先登录或检查您的网络");
      }
    }).catch((res) => {
      alert("错误：" + res);
    });
  },
  methods:{
    async pay(){
      console.log("点击支付，等待进入支付页");

      await this.submitOrder();

      await aliPay(this.orderPay).then((res)=>{

        console.log(res.data);
        //然后就将页面跳转到支付的界面
        const div = document.createElement('divPay');
        div.innerHTML = res.data;
        document.body.appendChild(div);
        document.forms[1].submit();
        // this.submitOrder();

      }).catch((res) => {
        alert("错误：" + res);
      });

    },
    regionChange(data){
      console.log(data);
      this.newOrder.shipCity = data.province.value + data.city.value +data.area.value; //成功过获取
      console.log(this.newOrder.address);
    },
    getNewOrderList(){

      let nowUsername= sessionStorage.getItem("nowUsername");

      getCartByUsername(nowUsername).then((res) =>{
        this.newOrderList = res.data.data;

        console.log("this.newOrderList")
        console.log(this.newOrderList)

        //填上newOrder里的itemList
        for(var i =0;i < this.newOrderList.length;i++){
          var item={
            itemId: this.newOrderList[i].itemId,
            quantity: this.newOrderList[i].qty,
            price: this.newOrderList[i].listPrice,
            iName: this.newOrderList[i].iName,
            iImage: this.newOrderList[i].iImage,
            iDescn: this.newOrderList[i].iDescn
          }
          this.newOrder.itemList[i] = item;
          this.newOrder.totalPrice += this.newOrderList[i].total;  //拿到总价格
        }
      })
    },
    getOrderId(){
      getAllOrders().then((res)=>{
        this.orderPay.orderId = res.data.data.length + 1;
        this.orderPay.totalPrice = this.newOrder.totalPrice;
        this.orderPay.orderName = this.orderPay.orderId.toString();
        var date=new Date();
        var dateTime=date.getTime();
        this.orderPay.orderDate = this.formatDate(dateTime);
      })
    },
    // 时间戳格式化
    formatDate(datetime) {
      var date = new Date(datetime);// 时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var year = date.getFullYear();
      var month = ('0' + (date.getMonth() + 1)).slice(-2);
      var sdate = ('0' + date.getDate()).slice(-2);
      var hour = ('0' + date.getHours()).slice(-2);
      var minute = ('0' + date.getMinutes()).slice(-2);
      var second = ('0' + date.getSeconds()).slice(-2);
      // 拼接
      var result = year + '-' + month + '-' + sdate + ' ' + hour + ':' + minute + ':' + second;
      // 返回
      return result;
    },
    //提交订单
    submitOrder(){
      postOrder(this.newOrder).then((res)=>{
          console.log(res);
        }).catch((res) => {
          alert("错误：" + res);
        });
    }
  }
}
